﻿
#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
#endregion

namespace PenguinForce
{
    /// <summary>
    /// SmokePlumeParticleSystem is a specialization of ParticleSystem which sends up a
    /// plume of smoke. The smoke is blown to the right by the wind.
    /// </summary>
    public class SnowflakeParticleSystem : ParticleSystem
    {

        private static Random random = new Random();


        public static Random Random
        {
            get { return random; }
        }
        

        public SnowflakeParticleSystem(Game game, SpriteBatch sprite, ContentManager content, int howManyEffects)
            : base(game, sprite, content, howManyEffects)
        {
        }

        /// <summary>
        /// Set up the constants that will give this particle system its behavior and
        /// properties.
        /// </summary>
        protected override void InitializeConstants()
        {
            textureFilename = "snowflake_half";

            minInitialSpeed = 20;
            maxInitialSpeed = 80;

            // we don't want the particles to accelerate at all, aside from what we
            // do in our overriden InitializeParticle.
            minAcceleration = 0;
            maxAcceleration = 0;
            
            // long lifetime, this can be changed to create thinner or thicker smoke.
            // tweak minNumParticles and maxNumParticles to complement the effect.
            minLifetime = 10.0f;
            maxLifetime = 20.0f;

            minScale = .05f;
            maxScale = .5f;

            minNumParticles = 7;
            maxNumParticles = 15;

            // rotate slowly, we want a fairly relaxed effect
            minRotationSpeed = -MathHelper.PiOver4 / 2.0f;
            maxRotationSpeed = MathHelper.PiOver4 / 2.0f;

            spriteBlendMode = SpriteBlendMode.AlphaBlend;

            DrawOrder = AlphaBlendDrawOrder;
        }

        public static float RandomBetween(float min, float max)
        {

            return min + (float)random.NextDouble() * (max - min);
        }

        /// PickRandomDirection is overriden so that we can make the particles always 
        /// move have an initial velocity pointing up.
        /// </summary>
        /// <returns>a random direction which points basically up.</returns>
        protected override Vector2 PickRandomDirection()
        {
            // Point the particles somewhere between 80 and 100 degrees.
            // tweak this to

            //float radians = RandomBetween((float)(MathHelper.Pi + .25), (float)(MathHelper.TwoPi - .25));
            // Set snowflakes to initially fall downward towards the bottom of the screen
            float radians = RandomBetween((float)(MathHelper.Pi *4 /3),(float)(MathHelper.Pi*4/2));
           
            Vector2 direction = Vector2.Zero;
            // from the unit circle, cosine is the x coordinate and sine is the
            // y coordinate. We're negating y because on the screen increasing y moves
            // down the monitor.
            direction.X = (float)Math.Cos(radians);
            direction.Y = -(float)Math.Sin(radians);
            return direction;
        }

        /// <summary>
        /// InitializeParticle is overridden to add the appearance of wind.
        /// </summary>
        /// <param name="p">the particle to set up</param>
        /// <param name="where">where the particle should be placed</param>
        protected override void InitializeParticle(Particle p, Vector2 where)
        {
            base.InitializeParticle(p, where);

            // the base is mostly good, but we want to simulate a little bit of wind
            // heading to the right.
            //p.Acceleration.X += RandomBetween(10, 15);
        }

        private float m_windCountdown = 10000.0f;
        public override void Update(GameTime gameTime)
        {
            m_windCountdown -= gameTime.ElapsedGameTime.Milliseconds;
            float random = RandomBetween(1.0f, 2.0f);
            for (int i = 0; i < particles.Length; i++)
            {

                if (m_windCountdown <= 0.0f)
                {
                    if (random > 1.5f)
                    {
                        particles[i].m_windIsOn = true;
                    }
                    else
                    {
                        particles[i].m_windIsOn = false;
                    }
                    m_windCountdown = 10000.0f;
                }
            }
            base.Update(gameTime);
        }
        
    }
}
